#include "analysistab.h"
#include "ui_analysistab.h"

AnalysisTab::AnalysisTab(QWidget *parent) :
    QWidget(parent), m_ui(new Ui::AnalysisTab),
    imgL(0), sCounter(new SeedCounter)
{    
    m_ui->setupUi(this);
    connect(m_ui->showButton, SIGNAL(clicked()), this, SLOT(analysis()));
}

AnalysisTab::~AnalysisTab()
{
    delete m_ui;
    if(sCounter) delete sCounter;
}

void AnalysisTab::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void AnalysisTab::addLoader(ImageLoader *loader)
{
    if(loader)
    {
        m_ui->gridLayout->addWidget(loader, 0, 0);
        imgL = loader;
    }
}

void AnalysisTab::showContent()
{
    if(imgL != 0)
        m_ui->gridLayout->addWidget(imgL, 0, 0);
}

void AnalysisTab::analysis()
{
    if(imgL)
    {
        imgL->clearDisplay();

        QString file = imgL->getCurrFile();
        if(file.length() > 0)
        {
            // 'file' is of particular types in which both top and bottom
            // petri dishes are visible
            if(sCounter->addFile(file))
            {
                m_ui->areaLabel->setText(QString::number(sCounter->avgArea()));
                m_ui->periLabel->setText(QString::number(sCounter->avgPeri()));
                m_ui->majorLabel->setText(QString::number(sCounter->avgMajor()));
                m_ui->minorLabel->setText(QString::number(sCounter->avgMinor()));
                m_ui->roundnessLabel->setText(QString::number(sCounter->avgRoundness()));
                m_ui->aspectLabel->setText(QString::number(sCounter->avgAspect()));
                m_ui->comboBox->clear();

                imgL->addResultImg(sCounter->getResultImg());
            }
            else
            {
                QMessageBox msgBox;
                msgBox.setText("This type of experiment is not supported!  ");
                msgBox.exec();
            }
        }
    }
}
